<template>
	<view class="home">
		<uni-popup ref="popupsuccess" type="message">
			<uni-popup-message type="success" message="刷新成功" :duration="2000"></uni-popup-message>
		</uni-popup>
		<uni-popup ref="popuperror" type="message">
			<uni-popup-message type="error" message="更新失败,请重试" :duration="2000"></uni-popup-message>
		</uni-popup>

		<!-- <list-item :list="artList"></list-item> -->
		<list-block :list="artList"></list-block>

		<view class="more" v-if="showButtonLoading">
			<uni-load-more :status="more" :content-text="contentText"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	export default {
		data() {
			return {
				artList: [], // 新闻信息
				more: 'loading',
				showButtonLoading: false,
				currentPage: 1,
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				}
			}
		},
		computed: {
			...mapState(['userinfo'])
		},
		watch: {
			// 检测userinfo的变化
			userinfo(newVal) {
				//userinfo 变好了
				console.log('watch userinfo变化了')
			}
		},
		onShow() {
			// 微信分享
			this.shareWx('', '我的收藏')
						
			console.log("this.userinfo", this.userinfo)
			this.getList();

		},
		methods: {


			//获取数据
			// is_pulldownrefresh = 0 原始加载
			// is_pulldownrefresh = 1 下拉刷新数据
			// is_pulldownrefresh = 2 上拉加载数据
			getList(is_pulldownrefresh = 0, page = this.currentPage, num = 5) {
				//用户id
				var user_id = this.userinfo.id;

				if (is_pulldownrefresh === 0 || is_pulldownrefresh === 1) {
					// 显示顶部加载
					uni.showLoading()
				} else if (is_pulldownrefresh === 2) {
					this.showButtonLoading = true
				}

				var _this = this

				this.$api.get_list({
					page: page,
					num: num,
					user_id: user_id,
					type: 2 //2表示这个用户收藏的文章
				}).then(res => {
					console.log(res.data.length)
					if (res.code === 1) {

						if (res.data.length == 0) {
							_this.showButtonLoading = true;
							//加载完成
							this.more = 'moMore'
						} else if (res.data.length < num) {
							//如果本次加载的数量小于num说明已经获取完了
							this.more = 'moMore'
						}

						_this.artList = _this.artList.concat(res.data)

						_this.currentPage = _this.currentPage + 1;

						if (is_pulldownrefresh === 1) {
							// is_pulldownrefresh = 1 下拉刷新数据
							setTimeout(() => {
								//停止加载
								uni.stopPullDownRefresh();

								//提示刷新成功
								_this.$refs.popupsuccess.open()

								//隐藏加载
								uni.hideLoading()

							}, 1000)
						} else if (is_pulldownrefresh == 2) {
							// is_pulldownrefresh = 2 上拉加载数据
							setTimeout(() => {
								//隐藏加载
								uni.hideLoading()

							}, 1000)
						} else {
							//隐藏加载
							uni.hideLoading()
						}

					} else {
						//提示刷新成功
						_this.$refs.popuperror.open()
						//隐藏加载
						uni.hideLoading()
					}


				}).catch(() => {

				})
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			console.log('refresh');
			this.getList(1);
		},
		// 上滑加载
		onReachBottom() {
			// this.more == moMore 时候表示已经加载完成,不需要再请求了
			if (this.more == 'moMore') {
				return;
			} else {
				this.getList(2);
			}
		},
	}
</script>

<style>

</style>
